var init = 0;var init_bgm = 0;
var init2 = 0;var init3 = 0;
var innerAudioContext;//背景音乐对象
var button1;
var button2;//按钮音乐对象
var time;//定时器赋值
var app = getApp();
Page({
  data: {
    request_url: '',//url地址
    openId:"",//用户名
    score: 0,//当前面具分数
    index: 0,//当前面具编号
    current: 0,//当前swiper编号
    leftHidden: 'none',//左箭头显示
    rightHidden: 'none',//右箭头显示
    start: "",//上色与重绘
    count:0,
    passScores:[],
    ms:[],
    scores:[],
    masks:[],
    hide:"none",//是否显示戴上面具
    code:0,//用户是否第一次进入
    aniDisplay: "none",//指导动画显示
    guideAnimation:{}
  },
  
  onLoad: function (options) {
    let vm = this
    //获取接口地址
    var request_url = wx.getStorageSync('request_url')
    var openId = wx.getStorageSync('openid')
    // var code = wx.getStorageSync('code')
    vm.setData({
      request_url: request_url
    })
    vm.setData({
      openId: openId,
      code: options.code
    })
    vm.getScoreMask()
    // vm.getMask()
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        vm.setData({
          windowWidth: res.windowWidth//可使用宽度
        })
        vm.setData({
          windowHeight: res.screenHeight//可使用高度
        })
      }
    });

    if (vm.data.score == 0) {
      vm.setData({
        start: "../../img/workbench/paint.png"
      });
    } else {
      vm.setData({
        start: "../../img/workbench/repaint.png"
      });
    }
  },

  //获取玩家历史面具及分数
  getScoreMask(){
    let vm = this
    wx.request({
      url: vm.data.request_url + '/MianJuJiang/mask/getMaskInfos/'+vm.data.openId,
      method: 'GET',
      success: function (res) {
        console.log("后台请求成功");
        console.log(res.data);
        
        var count = 0;
        for (var key in res.data["masks"]) {
          count++;
        }

        var scores = new Array(count)
        var masks = new Array(count)
        var passScores = new Array(count)
        var address = new Array()
        
        for(var j = 0; j < count; j++){
          var o = res.data["masks"][j];
          var index = o["difficulty"]
          scores[index] = res.data["scores"][index]
          passScores[index] = res.data["passScores"][index]
          masks[index] = o
          address[index] ={url: vm.data.request_url + '/' + o["path"]}
        }

        vm.setData({
          ms: masks,
          scores: scores,
          count: count,
          masks: address,
          passScores: passScores
        })

        //是否显示戴上面具
        var path = vm.data.ms[0].path
        if (path.substring(path.length - 9, path.length - 4) == 'ready') {
          console.log(path.substring(path.length - 9, path.length - 4))
          vm.setData({
            hide: "block"
          })
        } else {
          console.log(path.substring(path.length - 9, path.length - 4))
          vm.setData({
            hide: "none"
          })
        }
        //左滑右滑箭头显示
        if (vm.data.current != 0) {
          vm.setData({
            leftHidden: 'block'
          })
        }
        if (vm.data.current < vm.data.masks.length - 1) {
          vm.setData({
            rightHidden: 'block'
          })
        }
        if (vm.data.current != 0) {
          vm.setData({
            leftHidden: 'block'
          })
        }
        if (vm.data.current < vm.data.masks.length - 1) {
          vm.setData({
            rightHidden: 'block'
          })
        }
        vm.setData({
          score: vm.data.scores[vm.data.ms[vm.data.current].id]
        });
        if (vm.data.score == 0) {
          vm.setData({
            start: "../../img/workbench/paint.png"
          });
        } else {
          vm.setData({
            start: "../../img/workbench/repaint.png"
          });
        }


      },
      fail: function (res) {
        console.log("后台请求失败");
        console.log(res.data);
      }
    })
  },
  //佩戴面具
  wear: function () {
    let vm = this
    this.button("button1", true, false);
    wx.redirectTo({
      //跳转
      url: '../photo/photo?maskIndex=' + vm.data.ms[vm.data.current].id,
    })
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    let vm = this;
    
    // 是否显示戴上面具
    var path = vm.data.ms[e.detail.current].path
    vm.setData({
      // currentTab: e.detail.current,
      current: e.detail.current,
    });
    if (path.substring(path.length - 9, path.length-4)=='ready'){
      console.log(path.substring(path.length - 9, path.length - 4))
      vm.setData({
        hide:"block"
      })
    }else{
      console.log(path.substring(path.length - 9, path.length - 4))
      vm.setData({
        hide: "none"
      })
    }
    if (vm.data.current == 0) {
      vm.setData({
        leftHidden: 'none'
      })
    }
    else {
      vm.setData({
        leftHidden: 'block'
      })
    }
    if (vm.data.current < vm.data.masks.length - 1) {
      vm.setData({
        rightHidden: 'block'
      })
    } else {
      vm.setData({
        rightHidden: 'none'
      })
    }
    vm.setData({
      score: vm.data.scores[vm.data.ms[vm.data.current].id]
    });
    if (vm.data.score == 0) {
      vm.setData({
        start: "../../img/workbench/paint.png"
      });
    } else {
      vm.setData({
        start: "../../img/workbench/repaint.png"
      });
    }
  },
  //左滑箭头
  last: function (e) {
    this.button2("button2", false);
    let vm = this;
    if (vm.data.current > 0) {
      vm.setData({
        index: vm.data.current - 1,
        leftHidden: 'block'
      });
    }else{
      vm.setData({
        leftHidden: 'none'
      })
    }
    
  },
  //右滑箭头
  next: function (e) {
    this.button2("button2", false);
    let vm = this;
    if (vm.data.current < vm.data.count-1) {
      vm.setData({
        index: vm.data.current + 1,
        rightHidden: 'block'
      });
    } else {
      vm.setData({
        rightHidden: 'none'
      })
    }
  },

  //两个音效
  startbgAudio: function (u, a, l) {
    if (init == 0) {
      init++;
      innerAudioContext = wx.createInnerAudioContext()
      innerAudioContext.autoplay = a;
      var urlKey = u;
      var savedFilePath = wx.getStorageSync(urlKey);
      innerAudioContext.src = savedFilePath;
      innerAudioContext.loop = l;
    } else {
      innerAudioContext.autoplay = a;
      innerAudioContext.src = wx.getStorageSync(u);
      innerAudioContext.loop = l;
    }
  },
  stopbgAudio: function () {
    innerAudioContext.stop();
  },
  button: function (m, o) {
    if (init2 == 0) {
      init2++;
      button1 = wx.createInnerAudioContext()
      button1.autoplay = true;
      var urlKey = m;
      var savedFilePath = wx.getStorageSync(urlKey);
      button1.src = savedFilePath;
      button1.loop = o;
    } else {
      button1.autoplay = true;
      button1.src = wx.getStorageSync(m);
      button1.loop = o;
    }
  },
  button2: function (m, o) {
    if (init3 == 0) {
      init3++;
      button2 = wx.createInnerAudioContext()
      button2.autoplay = true;
      var urlKey = m;
      var savedFilePath = wx.getStorageSync(urlKey);
      button2.src = savedFilePath;
      button2.loop = o;
    } else {
      button2.autoplay = true;
      button2.src = wx.getStorageSync(m);
      button2.loop = o;
    }
  },
  //重绘&上色
  start: function () {
    this.button("button1", false);
    let vm = this;

    wx.redirectTo({
      url: '../play/play?maskIndex=' + vm.data.ms[vm.data.current].id + 
      '&maskNum=' + vm.data.ms[vm.data.current].num+
      '&passScore=' + vm.data.passScores[vm.data.ms[vm.data.current].id]+
      '&code='+vm.data.code,
    })
  },
  //显示动画
  onShow: function () {
    let vm = this
    var ii = 0
    if (vm.data.code == 0) {
      vm.setData({
        aniDisplay: "block"//显示指导教程
      })
      var guideAnimation = wx.createAnimation({
        duration: 100, // 默认为400     动画持续时间，单位ms
        timingFunction: 'ease-in-out',
        delay: 100,
      });
      time=setInterval(function () {
        //动画的脚本定义必须每次都重新生成，不能放在循环外
        guideAnimation.translate(40, 15).step({ duration: 1000 }).translate().step({ duration: 1000 });
        // 更新数据
        vm.setData({
          // 导出动画示例
          guideAnimation: guideAnimation.export(),
        })
        ++ii;
        console.log(ii);
      }.bind(vm), 2000);
    }
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭,清除定时器
    clearInterval(time)
  },

  footerTap: app.footerTap
})